package cn.darkhorse.basic.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.darkhorse.basic.mapper.QmsSampleBoxMapper;
import cn.darkhorse.basic.domain.QmsSampleBox;
import cn.darkhorse.basic.service.IQmsSampleBoxService;

/**
 * 样品盒管理Service业务层处理
 *
 * @author ytq
 * @date 2024-07-18
 */
@Service
public class QmsSampleBoxServiceImpl extends ServiceImpl<QmsSampleBoxMapper, QmsSampleBox> implements IQmsSampleBoxService
{
    @Autowired
    private QmsSampleBoxMapper qmsSampleBoxMapper;

    /**
     * 查询样品盒管理
     *
     * @param id 样品盒管理主键
     * @return 样品盒管理
     */
    @Override
    public QmsSampleBox selectQmsSampleBoxById(Long id)
    {
        return qmsSampleBoxMapper.selectQmsSampleBoxById(id);
    }

    /**
     * 查询样品盒管理列表
     *
     * @param qmsSampleBox 样品盒管理
     * @return 样品盒管理
     */
    @Override
    public List<QmsSampleBox> selectQmsSampleBoxList(QmsSampleBox qmsSampleBox)
    {
        return qmsSampleBoxMapper.selectQmsSampleBoxList(qmsSampleBox);
    }

    /**
     * 新增样品盒管理
     *
     * @param qmsSampleBox 样品盒管理
     * @return 结果
     */
    @Override
    public int saveQmsSampleBox(QmsSampleBox qmsSampleBox) {
        if (qmsSampleBox.getId() == null){
            Long count = lambdaQuery().eq(QmsSampleBox::getSampleBoxNumber, qmsSampleBox.getSampleBoxNumber()).count();
            if (count>0){
                throw new RuntimeException("盒号重复");
            }

            qmsSampleBox.setCreateTime(DateUtils.getNowDate());

            return qmsSampleBoxMapper.insertQmsSampleBox(qmsSampleBox);
        }else{
            Long count = lambdaQuery().eq(QmsSampleBox::getSampleBoxNumber, qmsSampleBox.getSampleBoxNumber()).ne(QmsSampleBox::getId, qmsSampleBox.getId()).count();
            if (count>0){
                throw new RuntimeException("盒号重复");
            }
            return qmsSampleBoxMapper.updateQmsSampleBox(qmsSampleBox);
        }
    }


    /**
     * 批量删除样品盒管理
     *
     * @param ids 需要删除的样品盒管理主键
     * @return 结果
     */
    @Override
    public int deleteQmsSampleBoxByIds(Long[] ids)
    {
        return qmsSampleBoxMapper.deleteQmsSampleBoxByIds(ids);
    }

    /**
     * 删除样品盒管理信息
     *
     * @param id 样品盒管理主键
     * @return 结果
     */
    @Override
    public int deleteQmsSampleBoxById(Long id)
    {
        return qmsSampleBoxMapper.deleteQmsSampleBoxById(id);
    }
}
